package com.purong.purong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mcxtzhang.swipemenulib.SwipeMenuLayout;
import com.purong.purong.R;
import com.purong.purong.bean.ArticleBean;
import com.purong.purong.util.CommonUtil;
import com.purong.purong.widget.JustifyTextView;
import com.yao.sdk.glide.GlideUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wenjiezeng on 2017/11/8.
 */
public class ArticleCollectAdapter extends RecyclerView.Adapter<ArticleCollectAdapter.ViewHolder> {
    private List<ArticleBean> articleList=new ArrayList<>();
    private Context mContext;
    private OnArticleItemClickListener mListener;
    public ArticleCollectAdapter(Context context){
        this.mContext=context;
    }
    public void setData(List<ArticleBean> list) {
        this.articleList = list;

    }
    public void setOnInfoItemClickListener(OnArticleItemClickListener listener){
        this.mListener=listener;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v;
        v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.information_item_slide, parent, false);
        // set the view's size, margins, paddings and item_circle parameters
        return  new ViewHolder(v);


    }

    @Override
    public void onBindViewHolder(final ArticleCollectAdapter.ViewHolder holder, final int position) {
        holder.title.setText(articleList.get(position).getTitle());
        holder.date.setText(articleList.get(position).getRelease_time());
        holder.browse.setText(CommonUtil.numConvert(Integer.valueOf(articleList.get(position).getBrowse()),1)+"阅读");
        if (articleList.get(position).getMaster_url()==null||articleList.get(position).getMaster_url().equals("")) {
            holder.img.setVisibility(View.GONE);
        }else {
            holder.img.setVisibility(View.VISIBLE);
            GlideUtil.getInstance().load(mContext,articleList.get(position).getMaster_url(),holder.img,true);
        }
        final int finalPosition = position;
        holder.mainLay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener!=null){
                    mListener.onArticleClick(finalPosition);
                }
            }
        });
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener!=null)
                    mListener.onDelete(finalPosition);
                holder.swipeMenuLayout.smoothClose();
            }
        });
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        //绑定每个CircleItem的控件引用到ViewHolder
        private SwipeMenuLayout swipeMenuLayout;
        private TextView title;
        private TextView browse,date;
        private TextView delete;
        private ImageView img;
        private LinearLayout mainLay;
        public ViewHolder(View v) {
            super(v);
            swipeMenuLayout=(SwipeMenuLayout)v.findViewById(R.id.swipe_menu_layout);
            title=(TextView)v.findViewById(R.id.title);
            browse=(TextView)v.findViewById(R.id.browse);
            date=(TextView)v.findViewById(R.id.date);
            img=(ImageView)v.findViewById(R.id.img);
            mainLay=(LinearLayout)v.findViewById(R.id.main_lay);
            delete=(TextView)v.findViewById(R.id.delete);
        }
    }



    @Override
    public int getItemCount() {
        return articleList.size();
    }


    public interface OnArticleItemClickListener {
        void onArticleClick(int position);
        void onDelete(int position);
    }
}
